package com.xjl.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xjl.dao.AdminDao;
import com.xjl.demo.Admin;
import com.xjl.util.DBUtil;

public class AdminDaoImpl implements AdminDao {
	//根据管理员账号和密码查询用户信息
	public Admin CheckAdminLoginDao(int adminid , String adminpwd) throws Exception {
		
		Connection conn = DBUtil.getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Admin admin = null;
		//进行查询数据库
		String sql = "select *from t_admin where adminid=? and adminpwd=?";
		stmt = conn.prepareStatement(sql);
		stmt.setInt(1,adminid);
		stmt.setString(2, adminpwd);
		rs = stmt.executeQuery();
		//返回查询结果集
		while(rs.next()){
			admin =new Admin();
			admin.setAdminid(rs.getInt("adminid"));
			admin.setAdminname(rs.getString("adminname"));
			admin.setAdminpwd(rs.getString("adminpwd"));
		}
		//关闭结果集
		DBUtil.close(rs, stmt, conn);
		return admin;
	}

	//根据用户信息修改管理员的密码
	public int ChangeAdminPwdDao(Admin admin, String newpwd) {
		Connection conn = null;
		PreparedStatement stmt = null;
		int index=0;
		try {
			conn = DBUtil.getConnection();
			String sql = "update t_admin set adminpwd=? where adminid=?  ";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1,newpwd);
			stmt.setInt(2, admin.getAdminid());
			index=stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(stmt, conn);
		}
		return index;
	}

	//查询所有管理员的信息
	public List<Admin> SelectAllAdmin() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Admin> list=new ArrayList<Admin>();//创建list集合，用于保持admin对象
		try {
			conn = DBUtil.getConnection();
			String sql = "select * from t_admin";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while(rs.next()){
				Admin admin =new Admin();
				admin.setAdminid(rs.getInt("adminid"));
				admin.setAdminname(rs.getString("adminname"));
				admin.setAdminpwd(rs.getString("adminpwd"));
				list.add(admin);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs,stmt, conn);
		}
		return list;
	}

	//删除管理员
	public int DeleteAdminById(int adminid) {
		Connection conn = null;
		PreparedStatement stmt = null;
		int index=0;
		try {
			conn = DBUtil.getConnection();
			String sql = "DELETE from t_admin where adminid=? ";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, adminid);
			index=stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(stmt, conn);
		}
		return index;
	}

	@Override
	public int SelectAdminById(int adminid) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs=null;
		int index = 0;
		try {
			conn = DBUtil.getConnection();
			String sql = "select count(*) from t_admin where adminid=? ";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, adminid);
			rs=stmt.executeQuery();
			if(rs.next()){
				index = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs,stmt, conn);
		}
		return index;
	}

	//新增管理员
	public int InsertnewAdminDao(Admin admin) {
		Connection conn = null;
		PreparedStatement stmt = null;
		int index=0;
		try {
			conn = DBUtil.getConnection();
			String sql = "insert  into `t_admin`values (?,?,?)";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, admin.getAdminid());
			stmt.setString(2, admin.getAdminname());
			stmt.setString(3, admin.getAdminpwd());
			index=stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(stmt, conn);
		}
		return index;
	}
							
}
